using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using System.Xml;
/// <revisionHistory>
/// <revision author="Moayad Mardini [mardini]" date="07-September-2006">
/// Create the form to bookmarks your favorites threads
/// </revision>
/// <revision author="Moayad Mardini [mardini]" date="08-September-2006">
/// Added the deletion ability
/// </revision>
/// </revisionHistory>
namespace DeliciousTest
{
    public partial class frmBookmark : Form
    {
        private Login loginForm;
        private Delicious BaseClass;
        public frmBookmark()
        {
            InitializeComponent();
        }
        public void PassCredentials()
        {
               if ((loginForm.password() != "") && (loginForm.username() != ""))
                   BaseClass.SetCredentials(loginForm.username(), loginForm.password());
               else
               {
                   MessageBox.Show("Please enter your credentials.", @"Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
                   loginForm.Show();
               }
        }
        private void frmBookmark_Load(object sender, EventArgs e)
        {
         BaseClass   = new Delicious();
         loginForm = new Login();
            loginForm.ShowDialog();
        }

        private void buttonTagIt_Click(object sender, EventArgs e)
        {
            this.PassCredentials();
            if ((this.textBoxThreadID.Text != "") && (this.textBoxDescription.Text != ""))
            {
                BaseClass.TagThread(@"Favorites", this.textBoxDescription.Text, this.textBoxThreadID.Text);
                DownloadBookmarks();
            }
            else
                MessageBox.Show("Please make sure all necessary inputs have been entered.");
        }

        private void buttonDownload_Click(object sender, EventArgs e)
        {
            DownloadBookmarks();
        }
        private void DownloadBookmarks()
        {
            listBoxBookmarkedThreadsDescription.Items.Clear();
            listBoxBookmarkedThreadsHref.Items.Clear();
            PassCredentials();
            XmlDocument doc = BaseClass.GetPostsUnderTag(@"MSDNForums.Favorites");
            XmlNodeList nodeList = doc.SelectNodes("/posts/post/@description");
            foreach (XmlNode n in nodeList)
            {
                listBoxBookmarkedThreadsDescription.Items.Add(n.Value);
            }
            doc = BaseClass.GetPostsUnderTag(@"MSDNForums.Favorites");
            nodeList = doc.SelectNodes("/posts/post/@href");
            foreach (XmlNode n in nodeList)
            {
                listBoxBookmarkedThreadsHref.Items.Add(n.Value);
            }
        }
        private void listBoxBookmarkedThreadsDescription_SelectedIndexChanged(object sender, EventArgs e)
        {
            if (listBoxBookmarkedThreadsDescription.Items.Count == listBoxBookmarkedThreadsHref.Items.Count)
            {
                listBoxBookmarkedThreadsHref.SelectedIndex = listBoxBookmarkedThreadsDescription.SelectedIndex;
            }
        }

        private void buttonDeleteBookmark_Click(object sender, EventArgs e)
        {
            if (listBoxBookmarkedThreadsHref.SelectedIndices.Count > 0)
            {
            PassCredentials();
            XmlDocument doc = BaseClass.DeleteBookmark(listBoxBookmarkedThreadsHref.SelectedItem.ToString());
            XmlNodeList nodeList = doc.SelectNodes("/result/@code");
            if (nodeList[0].Value   == @"done")
            {
                DownloadBookmarks();
            }
            else
            {
                MessageBox.Show(@"Something went wrong", @"Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
            }
        }
        }

        private void signInToAnotherAccountButton_Click(object sender, EventArgs e)
        {
            loginForm = new Login();
            loginForm.ShowDialog();
        }
    }
}